from concepts import *


class Path(Object):
    map      = Attribute()
    path_map = Attribute()
    position = Attribute()
    target   = Attribute()
    
    path = Attribute(writable = True)
    
    def setup(self):
        self.path = iter(self.find_path())
    
    def find_path(self):
        if self.position is None or self.target is None:
            return None
        
        if hasattr(self.target, 'exists') and not self.target.exists:
            return None
        
        return self.path_map.find_path(self.position, self.target)
    
    def next(self):
        if self.path is None:
            raise StopIteration
        
        return self.path.next()
    
    def __iter__(self):
        return self
